<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Class Project extends MY_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->data['controller'] = 'project';
        $this->load->model('Mproject');
		$this->checkAdmin($this->session->userdata('admin_id'));
    }
    
    public function detail()
    {
		$id = $this->getParamUri(2,1);
		$this->checkPerProject($id); // check Permision Project
		$this->data['data'] = $this->Mproject->getProjectDetail($id);
		$this->data['comment'] = $this->Mproject->getCommentP($id);
                
                $this->data['action'] = 'detail';
        $this->load->view('template/layout', $this->data);
    }
    
    public function add()
    {
        $this->checkLevel1();
        
        $this->load->library('form_validation');
        $this->form_validation->set_rules('project_name', 'Tên dự án', 'required');
        $this->form_validation->set_rules('content', 'Nội dung', 'required');
        $this->form_validation->set_rules('status', 'Trạng thái', 'required');
        $this->form_validation->set_rules('time_start', 'Thời gian bắt đầu', 'required');
        $this->form_validation->set_rules('time_end', 'Thời gian kết thúc', 'required');
        
        $this->data['users'] = $this->Mproject->getUserList();
        if($this->form_validation->run() === TRUE)
        {
            $project_name = $this->getParamString('project_name');
            $data = array(
                'project_name' => $this->getParamString('project_name'),
                'content' => $this->getParamString('content'),
                'status' => $this->getParamInt('status'),
                'time_start' => date('Y-m-d',strtotime(str_replace('/', '-', $this->getParamString('time_start')))),
                'time_end' => date('Y-m-d',strtotime(str_replace('/', '-', $this->getParamString('time_end')))),
                'linkdocs' => $this->getParamString('docs')
            );
            $users = $this->getParamArray('users');
            $idProject = $this->Mproject->addProject($data,$users);
            //tạo tiến độ 0% trong bảng avg trước với từng user
            $f = $this->Mproject->addAvg($idProject,$data,$users);
            if($f == '200'){
                die('<meta charset="utf-8"><script >alert("Thêm dự án thành công!");window.location="'.$this->data['baseurl'].'";</script>');
            }else{
				die('<meta charset="utf-8"><script >alert("Đã tồn tại tên dự án!");window.location="'.$this->data['baseurl'].'";</script>');
			}
        }
        
        $this->data['action'] = __FUNCTION__;
        $this->load->view('template/layout', $this->data);
    }
    
    public function edit()
    {
        $this->checkLevel1();
        
        $this->load->library('form_validation');
        $this->form_validation->set_rules('project_name', 'Tên dự án', 'required');
        $this->form_validation->set_rules('content', 'Nội dung', 'required');
        $this->form_validation->set_rules('status', 'Trạng thái', 'required');
        $this->form_validation->set_rules('time_start', 'Thời gian bắt đầu', 'required');
        $this->form_validation->set_rules('time_end', 'Thời gian kết thúc', 'required');
        
        $id = $this->getParamUri(2,1);
        $this->data['data'] = $this->Mproject->getProjectInfo($id);
        $this->data['users'] = $this->Mproject->getUserList();
        if($this->form_validation->run() === TRUE)
        {
            $project_name = $this->getParamString('project_name');
            $data = array(
                'project_name' => $this->getParamString('project_name'),
                'content' => $this->getParamString('content'),
                'status' => $this->getParamInt('status'),
                'time_start' => date('Y-m-d',strtotime(str_replace('/', '-', $this->getParamString('time_start')))),
                'time_end' => date('Y-m-d',strtotime(str_replace('/', '-', $this->getParamString('time_end')))), 
                'linkdocs' => $this->getParamString('docs')
            );
            $users = $this->getParamArray('users');
            //Get ngày bắt đầu và kết thúc trước khi sửa
            $arrDate=$this->db->query("SELECT time_start,time_end FROM project WHERE id = '$id'")->row();
            //Sửa phần trăm trước
            $f = $this->Mproject->editAvg($id,$data,$users,$arrDate);
            $f = $this->Mproject->editProject($id,$data,$users);
            
            if($f == '200'){
                die('<meta charset="utf-8"><script >alert("Cập nhật dự án thành công!");window.location="'.$this->data['baseurl'].'";</script>');
            }
        }
        
        $this->data['action'] = __FUNCTION__;
        $this->load->view('template/layout', $this->data);
    }
	
    public function pin(){
            $this->checkLevel1();
            $id = $this->getParamUri(2,1);
            if( in_array($id,array('',null)) ){
                    redirect('');
            }
            $run = $this->Mproject->savePinProject($id);
        if($run == '200'){
            die('<meta charset="utf-8"><script >alert("Cập nhật ghim dự án thành công!");window.location="'.$this->data['baseurl'].'";</script>');
        }
    }
        
    public function listProject()
    {
        $this->load->library('pagination');
        $config['per_page'] = $this->data['_per_page'] = 4;
        $this->data['page'] = $this->data['page_current'] = $this->getParamUri(2,1);
        $offset = (($this->getParamUri(2,1) - 1) * $config['per_page']) > 0 ? (($this->getParamUri(2,1) - 1) * $config['per_page']) : 0;
        $data = $this->Mproject->getListProject($config['per_page'],$offset);
        $config['base_url'] = base_url($this->uri->segment(1).'/');
        $config['total_rows'] = $data['count'];
        $config['uri_segment'] = 2;
        $config['uri_segment_page'] = $this->getParamUri(2,1);
        $config['suffix'] = '.html';
        $config['use_page_numbers'] = TRUE;
        $this->pagination->initialize($config);
        $this->data['data'] = $data['data'];
        
        $this->data['action'] = __FUNCTION__;
        $this->load->view('template/layout', $this->data);
    }
    
    public function reportProject()
    {
        $id=$this->getParamString("id");
        $this->data['data'] = $this->Mproject->getReportProject($id); //echo '<pre>';die(print_r($this->data['data']));
        $this->data['action'] = 'reportproject';
        $this->load->view('template/layout', $this->data);
    }
    
    public function deleteProject()
    {
        $id=$this->getParamString("id");
        $this->db->update('project',array('active' => '0'),array('id'=>$id));
        redirect(site_url().'list-project');
    }
    
    public function addCommentP(){

       $idUser=$this->session->userdata('admin_id');
       $project_id=$this->getParamString("project_id");
       $comment=$this->getParamString("comment");
       $run=$this->Mproject->addCommentP($idUser,$project_id,$comment);
       if($run != 200){
           echo "Comment thất bại";die;
       }
       else
       {
           echo "Comment thành công";die;
       }
    }
    public function delCommentP(){

       $idUser=$this->session->userdata('admin_id');
       if($this->session->userdata("level") != 1){
           echo "xóa thất bại";die;
       }
       $commentId=$this->getParamString("commentId");
       $this->db->query("DELETE FROM project_key_comment WHERE id = '$commentId'");
       echo "Xóa thành công";
    }
    
    public function addCommentM(){

       $idUser=$this->session->userdata('admin_id');
       $project_id=$this->getParamString("project_id");
       $module_id=$this->getParamString("module_id");
       $comment=$this->getParamString("comment");
       $run=$this->Mproject->addCommentM($idUser,$project_id,$comment,$module_id);
       if($run != 200){
           echo "Comment thất bại";die;
       }
       else
       {
           echo "Comment thành công";die;
       }
    }
    public function delCommentM(){

       $idUser=$this->session->userdata('admin_id');
       if($this->session->userdata("level") != 1){
           echo "Xóa thất bại";die;
       }
       $commentId=$this->getParamString("commentId");
       $this->db->query("DELETE FROM module_key_comment WHERE id = '$commentId'");
       echo "Xóa thành công";
    }
}
